clear
clc
close all

% get all normal filenames
filenames = getStaticTestFilenames();

plotThings = 0;

nfns = length(filenames);

% loop over filenames running simulation
whandle = waitbar(0,'Simulations Running');
for i = 1:nfns
    
    [dat(i).t,dat(i).x,maxdist(i),maxheight(i),loadtime(i),simTime(i)] = runSim([filenames{i}],plotThings);
    waitbar(i/nfns,whandle,'Simulations Running');
    
end
close(whandle);

%loop over all the data and plot stuff nicely 
figure
hold all
maxMaxDist = max(maxdist);
maxMaxHeight = max(maxheight);
axis([0,maxMaxDist,-10,10,0,maxMaxHeight])
for i = 1:nfns
    
    %set data after its passed the ground to NaN's so they don't show up on
    %the plot
    distVec = sqrt(dat(i).x(:,1).^2+dat(i).x(:,3).^2);
    for j = 1:length(dat(i).x(:,1))
        if distVec(j) > maxdist(i)
            dat(i).x(j,1:3) = [NaN,NaN,NaN];
        end
    end
    
    %plot everything
    phandle(i) = plot3(dat(i).x(:,1),dat(i).x(:,3),dat(i).x(:,2));
    ltitle{i} = ['Test ' num2str(i)];
end
%plot the mean distance
meanplot = [mean(maxdist),0,0];
phandle(i+1) = plot3(meanplot(1),meanplot(2),meanplot(3),'g*','MarkerSize',10);
ltitle{i+1} = 'Mean Dist Val';
legend(phandle,ltitle)
xlabel('x position')
ylabel('z position')
zlabel('y position')
grid on
view([-28 23])
    
% print the run time
ttime = sum(loadtime) + sum(simTime);
fprintf('\n\nTotal run time is %2.1f s\n',ttime)

%calculate means and standard deviations
meanDist = mean(maxdist);
mdFt = meanDist*3.28;
stdDist = std(maxdist);
stdDft = stdDist*3.28;

fprintf('\nMean Dist = %2.1fm (%3.1f ft), while the std dist = %2.1fm (%3.1f ft)\n',meanDist,mdFt,stdDist,stdDft)